<template>
  <!-- 隐私保护指引弹窗 -->
  <uni-popup ref="privacyShow" mode="center" width="600rpx" border-radius="20" mask-close-able="true">
    <view class="privacyBox">
      <view class="privacyTit">用户隐私保护提示</view>
      <view class="privacyDesc">
        感谢您的使用，在使用本小程序前，应当阅读并同意<text style="color: #409eff;"
          @click="openPrivacyContract">《用户隐私保护指引》</text>。当您点击同意并开始使用程序服务时，即表示您已理解并同意该条款内容，该条款将对您产生法律约束力。如您拒绝，将无法进入小程序。
      </view>
      <view class="privacyPost">
        <view class="refuseBtn">
          <navigator target="miniProgram" open-type="exit" @click="exit">不同意并退出</navigator>
        </view>
        <button v-if="agreeFlag" class="agreeBtn" open-type="agreePrivacyAuthorization"
          @agreeprivacyauthorization="agreeClick1">同意并继续</button>

        <button v-else class="agreeBtn" @click="agreeClick1">同意并继续</button>
      </view>
    </view>
  </uni-popup>
</template>

<script>
  export default {
    name: 'agreement',
    data() {
      return {
        agreeFlag: true
      };
    },
    onReady() {
      var _this = this;
      // 隐私政策
      if (wx.getPrivacySetting) {
        console.log('当前微信版本需要授权');
        this.agreeFlag = true
        _this.$refs['privacyShow'].open();
        wx.getPrivacySetting({
          success: res => {
            // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
            console.log(res, '返回来的===')
            if (!res.needAuthorization) {
              // 需要弹出隐私协议
              console.log('未同意')
              // _this.$refs['privacyShow'].open();
              return;
            } else {
              // 用户已经同意过隐私协议，所以不需要再弹出隐私协议，也能调用隐私接口
              console.log('yi同意')
              this.$emit('handleAgree');
            }
          },
          fail: () => { },
          complete: () => { }
        })
      } else {
        this.agreeFlag = false
      }
    },
    methods: {
      exit() {
        this.$refs['privacyShow'].close();
        this.agree = 0
        this.checkedFlag = false
        this.$emit('handleAgree', 0);
      },
      // 同意
      agreeClick1() {
        this.$emit('handleAgree', 1);
        this.$refs['privacyShow'].close();
      },
      showAgreeBox() {
        this.$refs['privacyShow'].open();
      },
      requirePrivacy() {
        console.log("jinru require privacy 0000")
        wx.requirePrivacyAuthorize((res) => {
          console.log("4560000", res)
        })
      },
      getPrivacySetting123() {
        console.log("同步信息结果0000")
        console.log(wx.onNeedPrivacyAuthorization, "同步信息结果1111")
        if (wx.onNeedPrivacyAuthorization) {
          wx.onNeedPrivacyAuthorization(resolve => {
            //获取用户微信侧同步结果
            getPrivacySetting(result => {
              console.log(result, "同步信息结果2222") // 返回结果为: result = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
              if (result.needAuthorization) {
                // 需要弹出隐私协议
                if (typeof privacyHandler === 'function') privacyHandler(resolve, result)
                console.log("获取微信储存的用户协议同步信息-用户未同意，请弹框处理-页面")
              }
            })
          })
        }
      },
      openPrivacyContract() {
        wx.openPrivacyContract({
          success: res => {
            console.log('openPrivacyContract success')
          },
          fail: res => {
            console.error('openPrivacyContract fail', res)
          }
        })
      },
      OpenAgreeClick() {
        this.$refs['privacyShow'].open();
      },
      // 同意
      agreeClick() {
        // 用户点击了同意，之后所有已声明过的隐私接口和组件都可以调用了

        this.$refs['privacyShow'].close();
        // 重新授权定位，调取父组件方法
        // this.$parent.getLocation();
      }
    }
  };
</script>

<style scoped lang="scss">
  .privacyBox {
    padding: 60rpx;
    box-sizing: border-box;

  }

  .privacyTit {
    font-size: 28.44rpx;
    font-weight: bold;
    color: #333;
    text-align: center;
    overflow: hidden;
  }

  .privacyDesc {
    font-size: 24.88rpx;
    color: #333;
    overflow: hidden;
    margin-top: 30rpx;
  }

  .privacyDesc text {
    color: #333;
  }

  .privacyPost {
    overflow: hidden;
    margin-top: 60rpx;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .privacyPost .refuseBtn {
    flex: 1;
    height: 80rpx;
    line-height: 80rpx;
    text-align: center;
    font-size: 24.88rpx;
    font-weight: bold;
    color: #fff;
    background: #333;
    border-radius: 40rpx;
    box-sizing: border-box;
    overflow: hidden;
  }

  .privacyPost .agreeBtn {
    flex: 1;
    height: 80rpx;
    line-height: 80rpx;
    text-align: center;
    font-size: 24.88rpx;
    font-weight: bold;
    color: #fff;
    background: blue;
    border-radius: 40rpx;
    box-sizing: border-box;
    overflow: hidden;
    margin-left: 20rpx;
  }
</style>